package cn.xuchunh;

public class ShellSort {
    public static void main(String[] args){
        int[] array = new int[]{3,4,53,5621,52,52,52,542,16,6513,5,163,561,165,163,165,165};
        sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + ", ");
        }
    }
    private static void sort(int[] array){
        int gap = 0;
        while(gap < array.length / 3) {
            gap = gap * 3 + 1;
        }
        int j = 0;
        for (; gap > 0; gap /= 3) {
            for (int i = gap; i < array.length; i++) {
                int temp = array[i];
                for (j = i - gap; j >= 0 && temp < array[j]; j = j - gap) {
                    array[j + gap] = array[j];
                }
                array[j + gap] = temp;
            }
        }
    }
}
